﻿using UnityEngine;
using UnityEngine.UI;

public class LevelSelect : MonoBehaviour {

	public Button btn_SetLevel;		//关卡确认按钮
	public int maxLevel;			//最大关数
	public Text maxLevelText;		//最大选择关数
	public InputField inputLevel;	//选择关数

	// Use this for initialization
	void Start () {
		if (PlayerPrefs.HasKey ("MaxLevel")) {
			maxLevel = PlayerPrefs.GetInt ("MaxLevel");
		} else if (PlayerPrefs.HasKey ("Level")) {
			maxLevel = PlayerPrefs.GetInt ("Level");
		} else {
			maxLevel = 1;
		}
		maxLevelText.text = maxLevel.ToString ();
		inputLevel.text = maxLevel.ToString ();
		inputLevel.onValueChanged.AddListener (delegate(string arg0) {
			if (arg0=="") {
				return;
			}
			int levelSelect = int.Parse(arg0);
			if (levelSelect>maxLevel || levelSelect<1) {
				inputLevel.text = maxLevel.ToString();
			}
		});

		btn_SetLevel.onClick.AddListener (()=>{
			int level = inputLevel.text.Trim()!=""?int.Parse(inputLevel.text):1;
			PlayerPrefs.SetInt("Level",level);
			this.gameObject.SetActive(false);
		});
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
